package folder.model.virtual;

import java.util.ArrayList;
import java.util.List;

import folder.model.link.Link;

public class VirtualLink
{
	// Virtual link
	
	public static VirtualLink virtualLink( int identifier, double bandwidth, VirtualNode virtualNodeA, VirtualNode virtualNodeB, VirtualNetwork virtualNetwork )
	{
		return new VirtualLink( identifier, bandwidth, virtualNodeA, virtualNodeB, virtualNetwork );
	}
	
	public VirtualLink( int identifier, double bandwidth, VirtualNode virtualNodeA, VirtualNode virtualNodeB, VirtualNetwork virtualNetwork )
	{
		this.identifier = identifier;
		this.bandwidth = bandwidth;
		this.virtualNodeA = virtualNodeA;
		this.virtualNodeB = virtualNodeB;
		this.virtualNetwork = virtualNetwork;
		
		this.connect();
	}
	
	private final int identifier;
	public int getIdentifier(){ return this.identifier; }
	
	private final double bandwidth;
	public double getBandwidth(){ return this.bandwidth; }
	
	private final VirtualNode virtualNodeA;
	public VirtualNode getVirtualNodeA(){ return this.virtualNodeA; }
	
	private final VirtualNode virtualNodeB;
	public VirtualNode getVirtualNodeB(){ return this.virtualNodeB; }
	
	private final VirtualNetwork virtualNetwork;
	public VirtualNetwork getVirtualNetwork(){ return virtualNetwork; }
	
    public String toString()
    {
    	String string = "(" + virtualNodeA + "-" + virtualNodeB + ")";
    	
    	if( links.size() > 0 )
    		string += " in " + links.toString();
    	
    	return string;
    }
	
    private void connect()
    {
    	this.virtualNodeA.addVirtualLink( this );
    	this.virtualNodeB.addVirtualLink( this );
    }
    
	// Allocation
	
	private List< Link > links = new ArrayList< Link >();
	public List< Link > getLinks(){ return this.links; }
	public void getLinks( List< Link > links ){ this.links = links; }
    public void addLink( Link link ){ links.add( link ); }
    
	public void release()
	{
		for( Link link: this.links )
			link.release( this );
		
		this.links = new ArrayList< Link >();
	}
	
}
